﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data.Linq
Imports System.Text
Imports DevComponents
Imports System.Data
Imports TNCDAL
Imports HTLibrary

Public Class ucDriver
    Dim db As New TanNamChinhDataContext
    Dim util As New Utilities
    Private isNewDriver As Boolean
    Private NewDriverID As Integer
    Private isEditDriver As Boolean
    Dim mdri As New MDrivers
    Dim indexRow As Integer = 0
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        DataGridViewHelper.DataGridView_InitEvent(gridDriver)
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub LoadImportir()
        util.FillDriverAllCombo(cbManager)
        util.FillDriverCombo(ColumnImporter1)
    End Sub
    Sub BindingToGrid()
        Dim query = mdri.getDriversList2()


        If Not IsNothing(query) Then
            Dim source As New BindingSource
            source.DataSource = query
            gridDriver.DataSource = source
            BindingNavigator1.BindingSource = source
            Try
                gridDriver.CurrentCell.Selected = False

            Catch ex As Exception

            End Try
        End If

    End Sub
    Sub BindingToGridSearch()
        Dim query = mdri.getDriversList2(txtDriverNameSearch.Text)


        If Not IsNothing(query) Then
            Dim source As New BindingSource
            source.DataSource = query
            gridDriver.DataSource = source
            BindingNavigator1.BindingSource = source

        End If
    End Sub
    Private Sub btnAddNewDri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        isNewDriver = True
        isEditDriver = False
        EnableDriverControl(True, True)
        btnAdd.Enabled = False
        btnSave.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnCancel.Enabled = True
        gridDriver.Enabled = False
        indexRow = 0
    End Sub
    Sub EnableDriverControl(ByVal value As Boolean, ByVal addnew As Boolean)
        txtDriverName.Enabled = value
        txtAddressDriver.Enabled = value
        txtPhoneNo.Enabled = value
        cbManager.Enabled = value
        txtIDDriver.Enabled = value
        btnCancel.Enabled = value
        btnSave.Enabled = value
        chkIsActive.Enabled = value

        If isNewDriver Then

            txtDriverName.Text = ""
            txtAddressDriver.Text = ""
            txtPhoneNo.Text = ""
            txtIDDriver.Text = ""
            chkIsActive.Checked = True
            txtDriverName.Focus()
        Else

        End If

    End Sub
   
    
    Private Sub FrmDriver_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BarSearch.Text = "Search"
        Bar2.Text = "Details"
        LoadImportir()
        BindingToGrid()
        'LoadSatuan()
        'isCellEdit = False
        isNewDriver = False
    End Sub

     

    Private Sub gridDriver_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridDriver.RowEnter
       
        Try
            Dim rw As Integer = e.RowIndex
            Dim dg As DataGridView = CType(sender, DataGridView)
            txtIDDriver.Text = dg.Item(0, rw).Value   'row.Item("id").ToString
            txtDriverName.Text = dg.Item(1, rw).Value 'row.Item("caption").ToString
            txtAddressDriver.Text = dg.Item(3, rw).Value 'row.Item("menu_desc").ToString
            If dg.Item(2, rw).Value Is Nothing Then
                txtPhoneNo.Text = ""
            Else
                txtPhoneNo.Text = dg.Item(2, rw).Value.ToString.Trim
            End If
            chkIsActive.Checked = CBool(dg.Item(5, rw).Value)
            If dg.Item(4, rw).Value Is Nothing Then
                cbManager.SelectedItem = 0
            Else
                cbManager.SelectedValue = dg.Item(4, rw).Value
            End If
        Catch ex As Exception

        End Try
     
    End Sub

    Private Sub btnEditDri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        isNewDriver = False
        isEditDriver = True
        EnableDriverControl(True, True)
        btnAdd.Enabled = False
        btnSave.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnCancel.Enabled = True
        txtDriverName.Focus()
        indexRow = gridDriver.CurrentRow.Index
        gridDriver.Enabled = False
    End Sub

    Private Sub gridDriver_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles gridDriver.DataBindingComplete
      

    End Sub

    Private Sub gridDriver_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles gridDriver.CellFormatting
        'Dim dg As DataGridView = CType(sender, DataGridView)
        'If e.RowIndex >= 0 Then
        '    If e.RowIndex <= gridDriver.Rows.Count - 1 Then
        '        Dim c As Color
        '        If CType(dg.Rows(e.RowIndex).HeaderCell.Value, Integer) Mod 2 <> 0 Then
        '            c = Color.CadetBlue
        '        Else
        '            c = Color.White
        '        End If
        '        e.CellStyle.BackColor = c
        '    End If
        'End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If isNewDriver Then
            'Thêm mới tài xế
            Dim obj As New tblDriver

            If txtDriverName.Text = "" Then
                MessageBox.Show("Nhập tên tài xế")
                txtDriverName.Focus()
                Return
            End If

            If cbManager.SelectedValue <> 0 Then
                obj.ID_Manager = cbManager.SelectedValue

            End If
            obj.DriverName = txtDriverName.Text

            If txtAddressDriver.Text <> "" Then
                obj.Address = txtAddressDriver.Text

            End If


            If txtPhoneNo.Text <> "" Then
                obj.PhoneNo = txtPhoneNo.Text.Trim()

            End If
            obj.IsActive = True
            mdri.InsertNewRecord(obj)
            EnableDriverControl(False, False)
        ElseIf isEditDriver Then
            'Sửa thông tin
            Dim obj As New tblDriver

            If txtDriverName.Text = "" Then
                MessageBox.Show("Nhập tên tài xế")
                txtDriverName.Focus()
                Return
            End If

            If txtIDDriver.Text = "" Then
                MessageBox.Show("Chưa chọn thông tin")
                txtDriverName.Focus()
                Return
            End If
            obj.ID_Driver = txtIDDriver.Text
            If cbManager.SelectedValue <> 0 Then
                obj.ID_Manager = cbManager.SelectedValue

            End If
            obj.DriverName = txtDriverName.Text

            If txtAddressDriver.Text <> "" Then
                obj.Address = txtAddressDriver.Text

            End If


            If txtPhoneNo.Text <> "" Then
                obj.PhoneNo = txtPhoneNo.Text

            End If
            obj.IsActive = chkIsActive.Checked
            mdri.UpdateRecord(obj)

            EnableDriverControl(False, False)
        End If
        BindingToGrid()
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False
        btnDelete.Enabled = True
        gridDriver.Enabled = True
        gridDriver.Rows(indexRow).Selected = True
        gridDriver.FirstDisplayedScrollingRowIndex = indexRow
        gridDriver.CurrentCell = gridDriver(1, indexRow)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        isNewDriver = False
        isEditDriver = False
        EnableDriverControl(False, False)
        BindingToGrid()
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnSave.Enabled = False
        btnDelete.Enabled = False
        btnCancel.Enabled = False
        gridDriver.Enabled = True
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Dim result As Integer
            result = util.ShowConfirm("Confirm", "Do you want to delete: " & txtDriverName.Text & " record?")
            If result = DialogResult.Yes Then
                Dim obj As New tblDriver
                obj.ID_Driver = txtIDDriver.Text
                obj.IsActive = False
                mdri.UpdateDeleteRecord(obj)
                BindingToGrid()
            End If
        Catch ex As Exception
            MessageBox.Show("Chưa chọn tài xế")
            Return
        End Try
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnSave.Enabled = False
        btnDelete.Enabled = True
        btnCancel.Enabled = False
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        BindingToGridSearch()
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        'XÓA
        BindingToGrid()
    End Sub

    
    Private Sub txtPhoneNo_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtPhoneNo.KeyPress
        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = ".") Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtDriverNameSearch_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtDriverNameSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            BindingToGridSearch()
        End If
    End Sub
End Class
